

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:gczn_hd/app/core/values/text_styles.dart';
import 'package:gczn_hd/app/core/widget/gczn_page.dart';
import 'package:gczn_hd/app/modules/eam/spotcheck/controllers/spotcheck_controller.dart';
import 'package:gczn_hd/app/routes/app_pages.dart';
import 'package:get/get.dart';
import '../../../../core/values/app_colors.dart';
import '../../../../core/values/app_values.dart';

class SpotPlan extends StatelessWidget{

  final SpotcheckController controller = Get.put(SpotcheckController());
  final Size boxSize = Size(360.w, 60.h);


  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          padding: EdgeInsets.symmetric(horizontal: AppValues.size40.w),
          margin: EdgeInsets.only(top: AppValues.size30.h),
          child: Obx(() => _buildSearchBar()),
        ),
        Expanded(
          flex: 1,
          child: GcznPage(
            easyController: controller.gcznPlanController.value,
            onRefresh: () async {
              await controller.onRefresh();
            },
            onLoadNextPage: () async {
              await controller.onLoadNextPage();
            },
            child: SingleChildScrollView(
              child: Container(
                padding: EdgeInsets.symmetric(horizontal: AppValues.size40.w, vertical: AppValues.size40.h),
                child: Wrap(
                  spacing: AppValues.size36, // 水平间距
                  runSpacing: AppValues.size36, // 垂直间距
                  children: [1,2,3,4,5,6,7].map((item) => _buildPlanItem(context, item)).toList(),
                ),
              ),
            ),
          ),
        )
      ],
    );
  }


  Widget _buildPlanItem(BuildContext context, int idx){

    return  GestureDetector(
      onTap: (){
        Get.toNamed(Routes.SPOTCHECK_DETAIL);
      },
      child: _buildPlanItmWrap(context,
        child: Column(
          children: [
            Text("注塑机A08", style: textStyle30.copyWith(fontWeight: FontWeight.w700),),
            _buildPlanMsgItem(title: "点检标准", value: "200T注塑机点检标准"),
            _buildPlanMsgItem(title: "点检频率", value: "15天/次（已超15天未点检）", isRed: idx % 2 > 0),
            _buildPlanMsgItem(title: "上次点检", value: "2024-2-28 19:42:51"),
            Container(
              margin: EdgeInsets.only(top: AppValues.size20.h),
              width: double.infinity,
              height: AppValues.size52.h,
              child: ElevatedButton(
                  clipBehavior: Clip.hardEdge,
                  onPressed: (){
                    //TODO
                  },
                  style: ElevatedButton.styleFrom(
                    backgroundColor: idx % 2 > 0 ? AppColors.successColor :AppColors.counterBtn,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.all(Radius.circular(AppValues.margin_12.r)),
                    ),
                  ),
                  child: Text("新增点检", style: textStyle,)
              ),
            )
          ],
        )
      )
    );
  }

  Widget _buildPlanItmWrap(BuildContext context, {required child}){
    return Container(
      width: (MediaQuery.of(context).size.width - 3 * AppValues.size36 - AppValues.size20) / 3, // 计算每个子项的宽度
      padding: EdgeInsets.only(left: AppValues.size28.w, right: AppValues.size28.w, top: AppValues.size20.w, bottom: AppValues.size20.w),
      decoration: BoxDecoration(
        border: Border.all(
          color: const Color.fromRGBO(39, 65, 80, 1),
          width: 1.0,
        ),
        borderRadius: BorderRadius.circular(AppValues.margin_12.r),
        color: AppColors.processBtn,
        boxShadow:  const [pressShadow],
      ),
      clipBehavior: Clip.hardEdge,
      child: child,
    );
  }

  Widget _buildPlanMsgItem({required String title, required String value, bool isRed = false}){
    final double labelWidth = 140.w;

    return  Container(
      margin: EdgeInsets.only(top: AppValues.size20.h),
      child: Row(
        children: [
          SizedBox( width: labelWidth, child: Text("$title：", style: textStyle20Opa8,), ),
          Expanded(child: Text(value, style: isRed ? fialTextStyle.copyWith(fontSize: AppValues.size24.sp) : textStyle,))
        ],
      ),
    );
  }


  Widget _buildSearchBar(){
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        _buildSearchSelectItem(title: "所属产线", selected: controller.selectLine.value, list: controller.seleces, onChanged: (_){
          controller.selectLine.value = _!;
        }),
        _buildSearchSelectItem(title: "所属工位", selected: controller.selectStation.value, list: controller.seleces, onChanged: (_){
          controller.selectStation.value = _!;
        }),
        _buildSearchSelectItem(title: "所属名称", selected: controller.selectMachine.value, list: controller.seleces, onChanged: (_){
          controller.selectMachine.value = _!;
        })
      ],
    );
  }

  Widget _buildSearchSelectItem({required String title, required String selected, required List<String> list, required Function(String? newValue) onChanged}){

    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        Text(title, style: textStyle,),
        SizedBox(width: AppValues.margin_10.w,),
        Container(
          width: boxSize.width,
          height: boxSize.height,
          decoration: BoxDecoration(
            border: Border.all(
              color: const Color.fromRGBO(39, 65, 80, 1),
              width: 1.0,
            ),
            borderRadius: BorderRadius.circular(AppValues.margin_12.r),
            color: AppColors.processBtn,//const Color.fromRGBO(12, 29, 46, 1),
          ),
          child: Row(
            children: [
              SizedBox(width: AppValues.size12.w,),
              Expanded(child: DropdownButton(
                value: selected,
                padding: EdgeInsets.zero,
                underline: Container(height: 0,),
                iconSize: 0,
                style:  submitButtonText.copyWith(fontSize: AppValues.size24.sp),
                items: list.map<DropdownMenuItem<String>>((String value) {
                  return DropdownMenuItem<String>(
                    value: value,
                    child: Text(value),
                  );
                }).toList(),
                onChanged: (String? newValue){
                  onChanged(newValue);
                },
                dropdownColor: AppColors.gcznPrimaryBlueBg,
              ),),
              SizedBox(width: 6.w,),
              Icon(Icons.arrow_forward_ios_outlined, size: AppValues.iconSize_18.sp, color: AppColors.colorWhite,),
              SizedBox(width: AppValues.size12.w,)
            ],
          ),
        )
      ],
    );



  }


}